/*
 * @description:客户端测试代码
 * @version: 1.0.1
 * @Author: xuqiulin
 * @Date: 2021-06-16 16:43:18
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-06-17 10:57:27
 */
package main

import (
	"fmt"
	"net"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	conn, err := net.Dial("tcp", "localhost:8888")
	if err != nil {
		// handle error
		fmt.Println(err)
		return
	}

	go handleRead(conn)
	go handleWrite(conn)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGINT, syscall.SIGTERM)
	sig := <-c
	fmt.Println(sig)
}

func handleRead(conn net.Conn) {
	b := make([]byte, 512)
	for {
		n, err := conn.Read(b)
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println("接受消息", string(b[:n]))

		time.Sleep(3 * time.Second)
	}
}

func handleWrite(conn net.Conn) {
	msg := "爱白菜的小昆虫"
	b := []byte(msg)
	for {
		n, err := conn.Write(b)
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println("发送成功", n)
	}
}
